package com.nbjtjc.safe.service.emg.handle;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.handle.EmgEventMapper;
import com.nbjtjc.safe.mapper.emg.handle.EmgHandleListPublicMapper;
import com.nbjtjc.safe.mapper.emg.handle.EmgHandlePublicMapper;
import com.nbjtjc.safe.model.emg.handle.EmgEvent;
import com.nbjtjc.safe.model.emg.handle.EmgHandleListPublic;
import com.nbjtjc.safe.model.emg.handle.EmgHandlePublic;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmgHandleListPublicService {
    @Autowired
    private EmgHandleListPublicMapper emgHandleListPublicMapper;

    @Autowired
    private EmgEventMapper emgEventMapper;

    @Autowired
    private EmgHandlePublicMapper emgHandlePublicMapper;
    public Result find(EmgHandleListPublic emgHandleListPublic) {
        Result result = new Result();
        PageHelper.startPage(emgHandleListPublic.getPageCurrent(), emgHandleListPublic.getPageSize());
        List<EmgHandleListPublic> emgHandleListPublics = emgHandleListPublicMapper.search(emgHandleListPublic);
        if (CollectionUtil.isNotEmpty(emgHandleListPublics)) {
            result.setData(new PageInfo<>(emgHandleListPublics));
        } else {
            result.setData(new PageInfo<>());
        }
        return result;
    }

    @Transactional(rollbackFor = Throwable.class)
    public Result delete(Integer id) {
        Result result = new Result();
        if (ObjectUtil.isNull(id) || id < 1) {
            result.setFalse("删除的id不能为空");
            return result;
        }
        emgHandleListPublicMapper.deleteByPrimaryKey(id);
        return result;
    }

    @Transactional(rollbackFor = Throwable.class)
    public Result add(EmgHandleListPublic emgHandleListPublic) {
        Result result = new Result();
        if (ObjectUtil.isNull(emgHandleListPublic)) {
            result.setFalse("关键字段不能为空");
        }
        int res = 0;
        res = emgHandleListPublicMapper.insertSelective(emgHandleListPublic);
        if (res == 0) {
            result.setFalse("新增或者更新失败");
        }
        result.setData(emgHandleListPublic);
        return result;
    }


    public Result update(EmgHandleListPublic emgHandleListPublic) {
        Result result = new Result();
        Map<String,Object> data=new HashMap<>();
        if (ObjectUtil.isNull(emgHandleListPublic) || ObjectUtil.isNull(emgHandleListPublic.getId())) {
            result.setFalse("关键字段不能为空");
        }
        int res = 0;
        emgHandleListPublic.setExecuteUserId(MyShiroUtils.getSessionUserId());
        emgHandleListPublic.setExecuteTime(new Date());
        res = emgHandleListPublicMapper.updateByPrimaryKeySelective(emgHandleListPublic);
        if (res == 0) {
            result.setFalse("更新失败");
        }
        result.setData(emgHandleListPublic);
        data.put("emgHandleListPublic",emgHandleListPublic);
        if(checkHandleListStatus(emgHandleListPublic)){
            data.put("handleListFinish",true);
        }else{
            data.put("handleListFinish",false);
        }
        result.setData(data);
        return result;
    }

    /**
     * 检查当前发布的应急处置的所有清单是否完结
     */
    private boolean checkHandleListStatus(EmgHandleListPublic emgHandleListPublic){
        emgHandleListPublic=emgHandleListPublicMapper.selectByPrimaryKey(emgHandleListPublic.getId());
        EmgHandlePublic emgHandlePublic=emgHandlePublicMapper.selectByPrimaryKey(emgHandleListPublic.getHandlePublicId());
        List<EmgHandleListPublic> emgHandleListPublics=emgHandleListPublicMapper
                .findByHandlePublicId(emgHandleListPublic.getHandlePublicId());
        if(CollectionUtil.isEmpty(emgHandleListPublics)){
            return false;
        }
        int count=0;
        for(EmgHandleListPublic handleList:emgHandleListPublics){
            if(handleList.getStatus().equals(1)){
                count++;
            }
        }
        if(emgHandleListPublics.size()==count){
            EmgEvent emgEvent=emgEventMapper.selectByPrimaryKey(emgHandlePublic.getEventId());
            if(ObjectUtil.isNull(emgEvent)){
                return false;
            }
//            emgEvent.setStatus(2);
//            emgEventMapper.updateByPrimaryKeySelective(emgEvent);结束事件的状态由前端触发
            return true;
        }
        return false;
    }
}
